﻿using JunQuanPrinter.DB;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JunQuanPrinter
{
    class CConfig
    {
        private String C_dbFile =  Application.StartupPath + "\\Data.db";  //Directory.GetCurrentDirectory()
        private CSQLite m_SQLite = null;
        private static CConfig m_Obj = null;
        private CCommSerial m_CommSer = new CCommSerial(); 
        public static CConfig getClass()
        {
            if(m_Obj == null)
            {
                m_Obj = new CConfig();
                m_Obj.m_SQLite = new CSQLite(m_Obj.C_dbFile);
                m_Obj.OpenDb();
                m_Obj.AutoConnCom();
            }

            return m_Obj;
        }


        public void AutoConnCom()
        {
            try
            {
                String sComPort = CConfigDB.getClass().getValue(CConfigDB.C_idx_Com);
                String sBaud = CConfigDB.getClass().getValue(CConfigDB.C_idx_Baud);

                Int32 Baud = Convert.ToInt32(sBaud);
                m_CommSer.setParam(sComPort, Baud);
                CCommad.getClass().SetParam(m_CommSer);
                if(!m_CommSer.Open())
                {
                    throw new Exception("打开串口失败！");
                }
            }
            catch(Exception ex)
            {
                CTool.ShowMsgErr(ex.Message);
            }
        }



        /// <summary>
        /// 打开数据库
        /// </summary>
        /// <returns></returns>
        public Boolean  OpenDb()
        {
            try
            {
                return CSQLite.getClass().Open();
            }
            catch(Exception ex)
            {
                MessageBox.Show("打开数据库失败，" + ex.Message);
                return false;
            }

        }

        



    }
}
